//
//  SGuideView.m
//  EasyRetail
//
//  Created by Samuel on 15/10/28.
//
//

#import "SGuideView.h"

@implementation SGuideView
{
    UIScrollView *scrollViews;
    UIButton *skipButton;
    UIButton *newButton;
}

+ (SGuideView*)shareSGuideViewAndShowInView:(UIView*)views andImageName:(NSString*)name,...NS_REQUIRES_NIL_TERMINATION;
{
    if(!views || !name)
    {
        return nil;
    }
    if(![[NSUserDefaults standardUserDefaults]objectForKey:@"samuelsohanson"]||![[[NSUserDefaults standardUserDefaults]objectForKey:@"samuelsohanson"]isEqualToString:@"yes"])
    {
        NSMutableArray *getAllObject = [NSMutableArray array];
        va_list arguments;
        va_start(arguments, name);
        NSString *getKey;
        if(name)
        {
            while((getKey = va_arg(arguments,NSString *)))
            {
                if (getKey) {
                    [getAllObject addObject:getKey];
                }
            }
        }
        va_end(arguments);
        [getAllObject insertObject:name atIndex:0];
        
        
        SGuideView *sgv = [[SGuideView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        sgv.imageArray = getAllObject;
        [views addSubview:sgv];
        [[NSUserDefaults standardUserDefaults]setObject:@"yes" forKey:@"samuelsohanson"];
        return sgv;
    }
    else
    {
        
        return nil;
    }
}

- (void)setImageArray:(NSMutableArray *)imageArray
{
    _imageArray = imageArray;
    scrollViews = [[UIScrollView alloc]initWithFrame:self.bounds];
    scrollViews.contentSize = CGSizeMake(imageArray.count * self.bounds.size.width, self.bounds.size.height);
    scrollViews.pagingEnabled = YES;
    scrollViews.showsHorizontalScrollIndicator = NO;
    scrollViews.bounces = NO;
    scrollViews.delegate = self;
    for (int i = 0;i <imageArray.count;i++) {
        NSString *imageName = [imageArray objectAtIndex:i];
        UIImageView *iV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        [iV setFrame:self.bounds];
        [iV setCenter:CGPointMake(iV.frame.size.width/2+(iV.frame.size.width)*i, iV.frame.size.height/2)];
        [scrollViews addSubview:iV];
    }
    [self addSubview:scrollViews];
    
    skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [skipButton setBackgroundColor:[UIColor clearColor]];
    [skipButton addTarget:self action:@selector(hideSelf:) forControlEvents:UIControlEventTouchUpInside];
    [skipButton setFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height/10)];
    
    newButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [newButton setBackgroundColor:[UIColor clearColor]];
    [newButton addTarget:self action:@selector(hideSelf:) forControlEvents:UIControlEventTouchUpInside];
    [newButton setFrame:CGRectMake(0, self.bounds.size.height/6*5, self.bounds.size.width, self.bounds.size.height/6)];
    [newButton setHidden:YES];

    [self addSubview:newButton];
    [self addSubview:skipButton];
}
- (void)hideSelf:(UIButton*)sender
{
    sender.userInteractionEnabled = NO;
    [UIView animateWithDuration:.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollViews.contentOffset.x>((self.imageArray.count-2) *self.bounds.size.width)) {
        if (newButton.hidden == YES) {
            [newButton setHidden:NO];
        }
    }
    else
    {
        if (newButton.hidden == NO) {
            [newButton setHidden:YES];
        }
    }
}
@end
